%%
%% Licensed to the Apache Software Foundation (ASF) under one
%% or more contributor license agreements. See the NOTICE file
%% distributed with this work for additional information
%% regarding copyright ownership. The ASF licenses this file
%% to you under the Apache License, Version 2.0 (the
%% "License"); you may not use this file except in compliance
%% with the License. You may obtain a copy of the License at
%%
%%   http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing,
%% software distributed under the License is distributed on an
%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
%% KIND, either express or implied. See the License for the
%% specific language governing permissions and limitations
%% under the License.
%%

-module(server).

-include("phptoerl_thrift.hrl").

%% EREDETI
-export([start/0, start/1, handle_function/2,
         stop/1, ping/0, send_message/1,send_message_binary/1,set_url/1,get_url/0,test_call_url/0]).






debug(Format, Data) ->
    error_logger:info_msg(Format, Data).

ping() ->
    debug("ping()",[]),
    ok.
   
send_message(Message) ->
    debug("incoming string ~s",[Message]),
    ok.    
    
send_message_binary(Binary_message) ->  
    debug("incoming binary ~s",[Binary_message]),
    ok.    
    
    
set_url(Url)     ->

    debug("URL from client ~s",[Url]),
    put(client_url, Url),
    Testurl=(get(client_url)),
    debug("data dictionary URL ~s",[Testurl]),
    ok.    
    
get_url() ->
    Testurl=(get(client_url)),
    debug("get_url() ~s",[Testurl]),
    Testurl.
    
    
test_call_url() ->
    
    
 
    case get(client_url) of
         undefined -> 
             throw(#invalidOperation{why="client url missing!"}); 
         Url ->
            debug("client url ~s",[Url]),
            
            %%  http://erlangexamples.com/2009/02/24/how-to-make-http-post/
            %% a gond ezzel csupán annyi, hogy csak GET paramétert ad át
            %% mivel a másik módon nem tudtam meghívni az oldalt, ez legalább ennyit tud
            %% és  a kliens oldalon egy egyszerű fájlbaírással jelzem, hogy sikeres volt a hívás
            %%http:get([Url])
            http:get("http://vakond/callback/")
        
        
        
        %       'hivatalos' megoldás
        %       parancssorban tökéletesen megy, de itt valamiért nem
             %%  inets:start(),
           
           
           %%    http:request(post, {"http://vakond/callback/", [],  "application/x-www-form-urlencoded", "adat=en&t2" }, [], [])
           
        
    end.
	
%%












start() ->
    start(9998).

start(Port) ->
    Handler   = ?MODULE,
    thrift_socket_server:start([{handler, Handler},
                                {service, phptoerl_thrift},
                                {port, Port},
                                {name, tutorial_server}]).

stop(Server) ->
    thrift_socket_server:stop(Server).

handle_function(Function, Args) when is_atom(Function), is_tuple(Args) ->
    case apply(?MODULE, Function, tuple_to_list(Args)) of
        ok -> ok;
        Reply -> {reply, Reply}
    end.
